home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / onetime.zip / FCR.C < prev    next >
C/C++ Source or Header  |  1995-04-26  |  2KB  |  74 lines

  1. /*************************************************************************/
  2. /*            CREATES RANDOM CODE.KEY FILE FOR TEXT FILE ENCODING        */
  3. /*                   range of random numbers 0 - 26                      */
  4. /*************************************************************************/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <io.h>
  9.  
  10. #define INVOCATION_ERROR 5
  11. #define FILE_ERR 10
  12. #define ARGCOUNT 2
  13. #define FILENAME argv[1]
  14. #define BUFFERSIZE 4096
  15. #define RANGE 26
  16. // ***        ^^  range of random "shifts" ***
  17.  
  18. int create_file( char *filename, long int count );
  19.  
  20. void main( int argc, char **argv )
  21. {
  22.    long len;
  23.    FILE *f;
  24.  
  25.      if( argc != ARGCOUNT )
  26.         {
  27.         puts(
  28.         "Form: fcr FILE_NAME, where FILE_NAME is the file to be encoded."
  29.         );
  30.  
  31.         exit ( INVOCATION_ERROR );
  32.         }
  33.  
  34.      f = fopen( FILENAME, "r" );
  35.  
  36.  
  37.      len = filelength( fileno( f ) );
  38.      printf( "\n\nCreating a CODE.KEY file more than %ld bytes long.\n", len );
  39.      create_file( "code.key", len );
  40.  
  41.      fclose ( f );
  42.  
  43.  
  44. }
  45.  
  46. int create_file( char *filename, long int count )
  47. {
  48.    register int r;
  49.    long n;
  50.    FILE *fp;
  51.  
  52.      randomize();
  53.  
  54.      if( NULL == ( fp = fopen( filename, "w" ) ) )
  55.        {
  56.        printf( "\nCannot open file %s\n", filename );         
  57.        exit ( FILE_ERR );
  58.        }
  59.  
  60.       if( setvbuf( fp, NULL, _IOFBF, BUFFERSIZE ) )
  61.          exit( FILE_ERR );
  62.  
  63.      for( n = 0; n < count; n++ )
  64.         {
  65.         r = random( RANGE );
  66.         fputc( r, fp );
  67.         }
  68.  
  69.      fclose( fp );
  70.  
  71.      return ( n );
  72.  
  73. }
  74.